package com.qhit.dao.impl;

import com.qhit.entity.Student;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生持久化层
 *
 * @author 村头老杨头
 * @version 1.0
 * @date 2021/9/27 17:17
 */
public class StudentDaoImpl {
    private String driver = "com.mysql.cj.jdbc.Driver";
    private String url = "jdbc:mysql://127.0.0.1:3306/edu";
    private String username = "root";
    private String password = "123456";
    //根据学生学号查询学生信息
    public Student getStudentById(String sno) throws ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException {
        Student student = new Student();
        //加载驱动
        Class.forName(driver);
        //创建连接
        Connection conn = DriverManager.getConnection(url, username, password);
        //SQL预处理
        String sql = "select * from student where sno=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        //问号填充
        ps.setObject(1,sno);
        //发送SQL并返回结果集
        ResultSet rs = ps.executeQuery();
        //解析
        if(rs.next()){
            //1、ResultSetMetaData元数据类，获得Student表所有的字段名
            ResultSetMetaData rsmd = rs.getMetaData();
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                String columnName = rsmd.getColumnName(i); //Sno
                //2、通过结果集rs.getObject(字段名)，获得字段对应的数据值
                Object columnValue = rs.getObject(columnName);
                //3、获得Student真实类的字段的反射对象Field, Field field = clz.getDeclaredField(字段名)
                Field field = Student.class.getDeclaredField(columnName);
                //4、通过字段反射对象Field.set(真实对象,数据值)，设置属性值。
                field.setAccessible(true);
                field.set(student,columnValue);
            }
        }
        //关闭
        rs.close();
        ps.close();
        conn.close();
        //返回
        return student;
    }
    public List<Student> getStudentList() throws Exception{
        List<Student> list = new ArrayList<>();
        //加载驱动
        Class.forName(driver);
        //创建连接
        Connection conn = DriverManager.getConnection(url, username, password);
        //SQL预处理    //改
        String sql = "select * from student";
        PreparedStatement ps = conn.prepareStatement(sql);
        //问号填充
//        ps.setObject(1,sno);
        //发送SQL并返回结果集
        ResultSet rs = ps.executeQuery();
        //解析
        while (rs.next()){
            Student student = new Student();
            //1、ResultSetMetaData元数据类，获得Student表所有的字段名
            ResultSetMetaData rsmd = rs.getMetaData();
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                String columnName = rsmd.getColumnName(i); //Sno
                //2、通过结果集rs.getObject(字段名)，获得字段对应的数据值
                Object columnValue = rs.getObject(columnName);
                //3、获得Student真实类的字段的反射对象Field, Field field = clz.getDeclaredField(字段名)
                Field field = Student.class.getDeclaredField(columnName);
                //4、通过字段反射对象Field.set(真实对象,数据值)，设置属性值。
                field.setAccessible(true);
                field.set(student,columnValue);
            }
            list.add(student);
        }
        //关闭
        rs.close();
        ps.close();
        conn.close();
        //返回
        return list;
    }
}
